package com.jingan.nursinghome.regulatory.http;

import android.util.Log;

import com.jingan.nursinghome.regulatory.MainActivity;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog2.CreateMicrometerTableDailog2;
import com.jingan.nursinghome.regulatory.model.Categirues2;
import com.jingan.nursinghome.regulatory.model.Categories;
import com.jingan.nursinghome.regulatory.model.Jinlaoyuang;
import com.jingan.nursinghome.regulatory.model.Linkup;
import com.jingan.nursinghome.regulatory.model.TimuType;
import com.jingan.nursinghome.regulatory.model.Topic;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.android.support.v7.lib.sin.crown.base.BaseActivityManager;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.CacheUtils;
import cn.android.support.v7.lib.sin.crown.utils.UtilCache;
import cn.android.support.v7.lib.sin.crown.utils.UtilHttp;
import cn.android.support.v7.lib.sin.crown.utils.JSONObjectUtils;
import cn.android.support.v7.lib.sin.crown.utils.UtilToast;

/**
 * Created by 彭治铭 on 2017/10/20.
 */

public class Http {
    private Http() {
    }

    static Http http;

    public static Http getInstance() {
        if (http == null) {
            http = new Http();
        }
        return http;
    }

//    String base = "http://meetuuu.com/";
//    String base="http://zhwlfwpt.jingan.gov.cn/";

    String base = "http://120.76.45.102:8080/";

    String urlLogin = base + "yanglaoapp/qwadmin/index.php/Home/Yanglaoapi/Login";//登陆
    String urlJly = base + "yanglaoapp/qwadmin/index.php/Home/Organs";//敬老院
    String urlLink = base + "yanglaoapp/qwadmin/index.php/Home/Linkup";//主要沟通人员
    String urlType = base + "yanglaoapp/qwadmin/index.php/Home/Subjecttype";//题目类型
    String urlOld = base + "yanglaoapp/qwadmin/index.php/Home/Special/lists.html";//oid 敬老院机构id 专项表敬老院题目

    String urlCategory = base + "yanglaoapp/qwadmin/index.php/Home/Subjecttypes/index";//获取题目类别
    String urlQian = base + "yanglaoapp/qwadmin/index.php/Home/Qfbsubject";//千分表日常表题目

    String urlCategories2 = base + "yanglaoapp/qwadmin/index.php/Home/Subjectchildclass/index";//二级菜单。

    String urlCommitQF = base + "yanglaoapp/qwadmin/index.php/Home/Qianfenlist/speciallist";//千分表提交
    String urlCommitRC = base + "yanglaoapp/qwadmin/index.php/Home/Dailylist/speciallist";//日常表提交
    String urlCommitZX = base + "yanglaoapp/qwadmin/index.php/Home/Special/speciallist";//专项表提交

    //登陆
    public void Login(String account, String pwd, UtilHttp.RequestCallBack callBack) {
        Map<String, String> map = new HashMap<>();
        map.put("account", account);
        map.put("pwd", pwd);
        UtilHttp.getInstance().sendPost(urlLogin, map, callBack);
    }

    //获取敬老院
    public void getJly() {
        UtilHttp.getInstance().sendGet(urlJly, null, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                try {
                    //Log.e("test", "敬老院数据:\t\n" + result);
                    JSONArray jsonArray = new JSONArray(result);
                    MicrometerTable.jlys.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Jinlaoyuang jinlaoyuang = new Jinlaoyuang();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        jinlaoyuang.setId(jsonObject.getString("id"));
                        jinlaoyuang.setName(jsonObject.getString("name"));
                        MicrometerTable.jlys.add(jinlaoyuang);
                    }
                    //缓存敬老院数据
                    CacheUtils.getInstance().put(MicrometerTable.getInstance().getJLYMark(), result);
                } catch (JSONException e) {
                    Log.e("test", "敬老院json数据获取异常:\t" + e.getMessage());
                }
            }

            @Override
            public void onFailure(String result) {
                try {
                    String string = CacheUtils.getInstance().getAsString(MicrometerTable.getInstance().getJLYMark());
                    if (string == null) {
                        return;
                    }
                    JSONArray jsonArray = new JSONArray(string);
                    MicrometerTable.jlys.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Jinlaoyuang jinlaoyuang = new Jinlaoyuang();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        jinlaoyuang.setId(jsonObject.getString("id"));
                        jinlaoyuang.setName(jsonObject.getString("name"));
                        MicrometerTable.jlys.add(jinlaoyuang);
                    }
                } catch (JSONException e) {
                    Log.e("test", "敬老院json数据获取异常2:\t" + e.getMessage());
                }
            }
        });
    }

    //根据题目大类，获取题目类型
    public void getCategory(final String rid) {
        Map<String, String> map = new HashMap<>();
        map.put("id", rid);
        UtilHttp.getInstance().sendPost(urlCategory, map, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").trim().equals("0")) {
                        JSONArray jsonArray = jsonObject.getJSONArray("list");
//                        Log.e("test","类别长度:\t"+jsonArray.length());
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
//                            Log.e("test","数据:\t"+jsonObject.toString());
                            String id = jsonObject1.getString("id");
                            String name = jsonObject1.getString("name");
                            Categories categories = new Categories();
                            categories.setId(id);
                            categories.setName(name);
                            MicrometerTable.getInstance().categories.add(categories);
                        }
                        //缓存数据
                        CacheUtils.getInstance().put(MicrometerTable.getInstance().getCategoriesMark(rid), JSONObjectUtils.getInstance().ArrayListToJSON(MicrometerTable.getInstance().categories));
                    }
                } catch (JSONException e) {
                    Log.e("test", "题目类型json数据解析异常:\t" + e.getMessage());
                }
            }

            @Override
            public void onFailure(String result) {
                Log.e("test", "大类题目类型网络获取失败:\t" + result);
                JSONArray jsonArray = CacheUtils.getInstance().getAsJSONArray(MicrometerTable.getInstance().getCategoriesMark(rid));//获取本地缓存
                if (jsonArray != null) {
                    MicrometerTable.getInstance().categories = JSONObjectUtils.getInstance().getArrayList(jsonArray, Categories.class);
                }
            }
        });
    }

    //千分表日常表题目
    public void getQian() {
        UtilHttp.getInstance().sendGet(urlQian, null, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                //Log.e("test","总数据:\t"+result);
                try {
                    JSONArray jsonArray = new JSONArray(result);
                    int inde = 1;

                    MicrometerTable.getInstance().topicsAll.clear();

                    for (int i = 0; i < jsonArray.length(); i++) {
                        //Log.e("test", "循环：\t" + i);
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String id = jsonObject.getString("id");
                        String question = jsonObject.getString("title");//题目问题
                        String maxScore = jsonObject.getString("subject_score");//分数。最大分数
                        String standard = jsonObject.getString("subject_standard");//得分标准
                        String rid = jsonObject.getString("rid");//题目类型id 1静安区 2闸北区
                        //Log.e("test","题目类型:\t"+rid);
                        String category = jsonObject.getString("subject_fname");//类别
                        String categoryID = jsonObject.getString("subject_f");//类别ID
                        String subject_typename = jsonObject.getString("subject_typename");//标题，分数总集。如: "1、院内环境--- 5分"

                        String subject_type = jsonObject.getString("subject_type");//标题id,二级分类ID

                        //Log.e("test", "subject_typename:\t" + subject_typename + "\tid:\t" + id);
                        String titleIndex = subject_typename.substring(0, subject_typename.indexOf("、")).trim();//小标题序号
                        String index = null;//小标题下标
                        //Log.e("test", "titleIndex:\t" + titleIndex + "\tinde:\t" + inde);
                        if (Integer.valueOf(titleIndex) == inde) {
                            index = "0";//表示小标题开头
                            inde++;
                        } else {
                            index = "1";
                        }
                        String title = subject_typename.substring(subject_typename.indexOf("、") + 1, subject_typename.indexOf("--"));//小标题题目
                        String tscore = subject_typename.substring(subject_typename.indexOf("--"), subject_typename.indexOf("分"));//小标题分数
                        String count = (i + 1) + "";//题目顺序

                        Topic topic = new Topic();
                        topic.setId(id);
                        topic.setCategory(category);
                        topic.setCategoryID(categoryID);
                        topic.setTitleIndex(titleIndex);
                        topic.setTitle(title);
                        topic.setTitleID(subject_type);
                        topic.setTscore(tscore);
                        topic.setIndex(index);
                        topic.setCount(count);
                        topic.setQuestion(question);
                        topic.setStandard(standard);
                        topic.setMaxScore(maxScore);
                        topic.setRid(rid);
                        MicrometerTable.getInstance().topicsAll.add(topic);
                    }
                    //缓存
                    CacheUtils.getInstance().put(MicrometerTable.getInstance().getTopicsALLMark(), JSONObjectUtils.getInstance().ArrayListToJSON(MicrometerTable.getInstance().topicsAll));
                    //Log.e("test", "总数据：\t" + MicrometerTable.getInstance().topicsAll.size());
                } catch (JSONException e) {
                    Log.e("test", "千分表提交json异常:\t" + e.getMessage());
                }
            }

            @Override
            public void onFailure(String result) {
                Log.e("test", "千分表总数据网络获取失败:\t" + result);
                JSONArray jsonArray = CacheUtils.getInstance().getAsJSONArray(MicrometerTable.getInstance().getTopicsALLMark());//获取本地缓存
                if (jsonArray != null) {
                    MicrometerTable.getInstance().topicsAll = JSONObjectUtils.getInstance().getArrayList(jsonArray, Topic.class);
                }
            }
        });
    }

    //获取主要沟通人员
    public void getLinkUP() {
        UtilHttp.getInstance().sendGet(urlLink, null, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                try {
                    //Log.e("test","数据:\t\n"+result);
                    JSONArray jsonArray = new JSONArray(result);
                    MicrometerTable.linkups.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Linkup linkup = new Linkup();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        linkup.setId(jsonObject.getString("id"));
                        linkup.setName(jsonObject.getString("name"));
                        linkup.setLtype(jsonObject.getString("ltype"));
                        MicrometerTable.linkups.add(linkup);
                    }
                    //缓存敬老院数据
                    CacheUtils.getInstance().put(MicrometerTable.getInstance().getLinkUPMark(), result);
                } catch (JSONException e) {
                    Log.e("test", "主要沟通人员json数据获取异常:\t" + e.getMessage());
                }
            }

            @Override
            public void onFailure(String result) {
                try {
                    String string = CacheUtils.getInstance().getAsString(MicrometerTable.getInstance().getLinkUPMark());
                    if (string == null) {
                        return;
                    }
                    JSONArray jsonArray = new JSONArray(string);
                    MicrometerTable.linkups.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Linkup linkup = new Linkup();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        linkup.setId(jsonObject.getString("id"));
                        linkup.setName(jsonObject.getString("name"));
                        linkup.setLtype(jsonObject.getString("ltype"));
                        MicrometerTable.linkups.add(linkup);
                    }
                } catch (JSONException e) {
                    Log.e("test", "主要沟通人员json数据获取异常2:\t" + e.getMessage());
                }
            }
        });
    }

    //获取题目类型
    public void getTimuTypes() {
        UtilHttp.getInstance().sendGet(urlType, null, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                try {
                    //Log.e("test","题目类别:\t"+result);
                    JSONArray jsonArray = new JSONArray(result);
                    MicrometerTable.timuTypes.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        TimuType jinlaoyuang = new TimuType();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        jinlaoyuang.setId(jsonObject.getString("id"));
                        jinlaoyuang.setQuname(jsonObject.getString("quname"));
                        MicrometerTable.timuTypes.add(jinlaoyuang);
                    }
                    //缓存敬老院数据
                    CacheUtils.getInstance().put(MicrometerTable.getInstance().getTimuTypeMark(), result);
                } catch (JSONException e) {
                    Log.e("test", "题目类型json数据获取异常:\t" + e.getMessage());
                }
            }

            @Override
            public void onFailure(String result) {
                try {
                    String string = CacheUtils.getInstance().getAsString(MicrometerTable.getInstance().getTimuTypeMark());
                    if (string == null) {
                        return;
                    }
                    JSONArray jsonArray = new JSONArray(string);
                    MicrometerTable.timuTypes.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        TimuType jinlaoyuang = new TimuType();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        jinlaoyuang.setId(jsonObject.getString("id"));
                        jinlaoyuang.setQuname(jsonObject.getString("quname"));
                        MicrometerTable.timuTypes.add(jinlaoyuang);
                    }
                } catch (JSONException e) {
                    Log.e("test", "题目类型json数据获取异常2:\t" + e.getMessage());
                }
            }
        });
    }

    //获取专项表数据,参数二，回调返回，true 数据已经加载完成
    public void getDataZX(String oid, final BaseListenner<Boolean> B) {
        Map<String, String> map = new HashMap();
        map.put("oid", oid);
        //Log.e("test","静脑院ID:\t"+oid);
        UtilHttp.getInstance().sendPost(urlOld, map, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                //Log.e("test", "专项表数据:\t" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").equals("0")) {
                        MicrometerTable.getInstance().topicsZX.clear();
                        //数据返回正确
                        String list = jsonObject.getString("list");
                        JSONArray jsonArray = new JSONArray(list);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            Topic topic = new Topic();
                            JSONObject object = jsonArray.getJSONObject(i);

                            topic.setId(object.getString("id"));
                            topic.setQuestion(object.getString("title"));//题目名称
                            topic.setStype(object.getString("sp_type"));//题目状态，1未完成 2已完成
                            //topic.setCount(object.getString("o"));//题目排序 靠不住
                            //Log.e("test","服务器序号:\t"+object.getString("o"));
                            //Log.e("test", "序号:\t" + (i + 1) + "");
                            topic.setCount((i + 1) + "");

                            object.getString("oid");//关联养老院ID
                            //object.getString("change_next");//整改次数(每次整改累计1
                            MicrometerTable.getInstance().topicsZX.add(topic);
                        }

                        //获取本地缓存【与本地进行对比，数据同步】
                        JSONArray jsonArray2 = CacheUtils.getInstance().getAsJSONArray(MicrometerTable.getInstance().getTopicsMarkZhuangXiang());
                        if (jsonArray2 != null) {
                            List<Topic> topicsZX2 = JSONObjectUtils.getInstance().getArrayList(jsonArray2, Topic.class);
                            int size = topicsZX2.size();
                            if (MicrometerTable.getInstance().topicsZX.size() < size) {
                                size = MicrometerTable.getInstance().topicsZX.size();//数量始终以最新的数据大小为标准
                            }
                            for (int i = 0; i < size; i++) {
                                if (MicrometerTable.getInstance().topicsZX.size() < i) {
                                    break;
                                }
                                //Log.e("test", "缓存题目:\t" + topicsZX2.get(i).getQuestion() + "\t新题目:\t" + MicrometerTable.getInstance().topicsZX.get(i).getQuestion());
                                if (topicsZX2.get(i).getQuestion().equals(MicrometerTable.getInstance().topicsZX.get(i).getQuestion())) {
                                    //Log.e("test", "题目相同：\t序号:\t" + i);
                                    //MicrometerTable.getInstance().topicsZX.remove(i);
                                    //MicrometerTable.getInstance().topicsZX.add(i, topicsZX2.get(i));
                                    MicrometerTable.getInstance().topicsZX.get(i).setRemark(topicsZX2.get(i).remark);//备注
                                    MicrometerTable.getInstance().topicsZX.get(i).setZhengai(topicsZX2.get(i).getZhengai());//整改
                                }
                            }
                        }

                        //缓存专项表的数据
                        MicrometerTable.getInstance().saveTOpicsZX();
                    } else {
                        //没有数据，或错误。
                        if (MicrometerTable.getInstance().topicsZX != null) {
                            MicrometerTable.getInstance().topicsZX.clear();//原有数据清空
                        }
                    }
                    if (B != null) {
                        B.onResult(true);
                    }
                } catch (JSONException e) {
                    Log.e("test", "专项表数据加载异常:\t" + e.getMessage());
                    UtilToast.showToastView(e.getMessage());
                }
            }

            @Override
            public void onFailure(String result) {
                //Log.e("test", "专项表数据加载异常2:\t" + result);
                //获取本地缓存
                JSONArray jsonArray = CacheUtils.getInstance().getAsJSONArray(MicrometerTable.getInstance().getTopicsMarkZhuangXiang());
                if (jsonArray != null) {
                    MicrometerTable.getInstance().topicsZX.clear();
                    MicrometerTable.getInstance().topicsZX = JSONObjectUtils.getInstance().getArrayList(jsonArray, Topic.class);
                }
                if (B != null) {
                    B.onResult(true);
                }
            }
        });
    }

    //千分表巡检信息提交
    public void CommitQF(Map map, Map file, final BaseListenner<String> baseListenner) {
        UtilHttp.getInstance().sendPostFile(urlCommitQF, map, file, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                String mession = "提交失败";
                try {
                    Log.e("test", "千分表提交：\t" + result);
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").equals("0")) {
                        //专项表巡检信息提交成功
                        Log.e("test", "提交成功");
                        mession = "提交成功";
                    } else {
                        mession = "提交错误:\t" + jsonObject.getString("errmsg");
                        Log.e("test", "千分表提交错误：\t" + jsonObject.getString("errmsg"));
                    }
                } catch (JSONException e) {
                    mession = "提交错误:\t" + e.getMessage();
                    Log.e("test", "千分表提交JSON数据解析出错:\t" + e.getMessage());
                }
                if (baseListenner != null) {
                    baseListenner.onResult(mession);
                }
            }

            @Override
            public void onFailure(String result) {
                if (baseListenner != null) {
                    if (result.contains("No address associated with hostname") || result.contains("No address") || result.contains("with hostname")) {
                        baseListenner.onResult("提交失败，没有网络");
                    } else {
                        baseListenner.onResult("服务器异常:\t" + result);
                    }
                }
                Log.e("test", "千分表提交异常2：\t" + result);
            }
        });
    }

    //日常表巡检信息提交
    public void CommitRC(Map map, Map file, final BaseListenner<String> baseListenner) {
        UtilHttp.getInstance().sendPostFile(urlCommitRC, map, file, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                String mession = "提交失败";
                try {
                    Log.e("test", "日常表提交：\t" + result);
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").equals("0")) {
                        //专项表巡检信息提交成功
                        Log.e("test", "提交成功");
                        mession = "提交成功";
                    } else {
                        mession = "提交错误:\t" + jsonObject.getString("errmsg");
                        Log.e("test", "日常表提交错误：\t" + jsonObject.getString("errmsg"));
                    }
                } catch (JSONException e) {
                    mession = "提交错误:\t" + e.getMessage();
                    Log.e("test", "日常表提交JSON数据解析出错:\t" + e.getMessage());
                }
                if (baseListenner != null) {
                    baseListenner.onResult(mession);
                }
            }

            @Override
            public void onFailure(String result) {
                if (baseListenner != null) {
                    if (result.contains("No address associated with hostname") || result.contains("No address") || result.contains("with hostname")) {
                        baseListenner.onResult("提交失败，没有网络");
                    } else {
                        baseListenner.onResult("服务器异常:\t" + result);
                    }
                }
                Log.e("test", "日常表提交异常2：\t" + result);
            }
        });
    }

    //专项表巡检信息提交
    public void CommitZX(Map map, Map file, final BaseListenner<String> baseListenner) {
        UtilHttp.getInstance().sendPostFile(urlCommitZX, map, file, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                String mession = "提交失败";
                try {
                    Log.e("test", "专项表提交：\t" + result);
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").equals("0")) {
                        //专项表巡检信息提交成功
                        Log.e("test", "提交成功");
                        mession = "提交成功";
                    } else {
                        mession = "提交错误:\t" + jsonObject.getString("errmsg");
                        Log.e("test", "专项表提交错误：\t" + jsonObject.getString("errmsg"));
                    }
                } catch (JSONException e) {
                    mession = "提交错误:\t" + e.getMessage();
                    Log.e("test", "专项表提交JSON数据解析出错:\t" + e.getMessage());
                }
                if (baseListenner != null) {
                    baseListenner.onResult(mession);
                }
            }

            @Override
            public void onFailure(String result) {
                if (baseListenner != null) {
                    if (result.contains("No address associated with hostname") || result.contains("No address") || result.contains("with hostname")) {
                        baseListenner.onResult("提交失败，没有网络");
                    } else {
                        baseListenner.onResult("服务器异常:\t" + result);
                    }
                }
                Log.e("test", "专项表提交异常2：\t" + result);
            }
        });
    }

    //网络获取千分表历史整改题目。本地缓存为空时调用
    //参数二，表示调用完成。
    public void getHostoryQFTopics(String oid, String rid, final BaseListenner baseListenner) {//敬老院ID
        String url = base + "yanglaoapp/qwadmin/index.php/Home/QfbRectificationsubject";
        if (url == null) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("oid", oid);
        map.put("rid", rid);
        UtilHttp.getInstance().sendPost(url, map, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                try {
                    //Log.e("test", "历史整改:\t" + result);
                    JSONArray jsonArray = new JSONArray(result);
                    MicrometerTable.getInstance().topicsHostoryQF.clear();
                    if (jsonArray != null && jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            Topic topic = new Topic();
                            String id = jsonObject.getString("id");//ID
                            String question = jsonObject.getString("title");//题目问题
                            String maxScore = jsonObject.getString("subject_score");//分数。最大分数
                            String cscore = jsonObject.getString("scoring");//当前分数

                            if (cscore == null || cscore.trim().equals("")) {
                                cscore = "0";
                            }

                            topic.setId(id);
                            topic.setQuestion(question);
                            topic.setMaxScore(maxScore);
                            topic.setCscore(cscore);
                            topic.setZhengai("2");//计划整改
                            topic.setCount((i + 1) + "");//题号
                            MicrometerTable.getInstance().topicsHostoryQF.add(topic);
                        }
                        //缓存千分表历史整改记录
                        CacheUtils.getInstance().put(MicrometerTable.getInstance().getTopicsHostoryQFMark(), JSONObjectUtils.getInstance().ArrayListToJSON(MicrometerTable.getInstance().topicsHostoryQF));
                    } else {
                        CacheUtils.getInstance().remove(MicrometerTable.getInstance().getTopicsHostoryQFMark());//清除本地历史整改。以服务器同步
                        Log.e("test", "历史整改为空");
                    }
                } catch (JSONException e) {
                    Log.e("test", "历史整改JSON数据解析出错:\t" + e.getMessage());
                }
                if (baseListenner != null) {
                    baseListenner.onResult(null);
                }
            }

            @Override
            public void onFailure(String result) {
                MicrometerTable.getInstance().getHostoryQFTopics();//换取本地缓存
                if (baseListenner != null) {
                    baseListenner.onResult(null);
                }
                Log.e("test", "千分表历史整改服务器反问异常" + result);
            }
        });
    }

    //网络获取日常表历史整改题目。本地缓存为空时调用。
    public void getHostoryRCTopics(String oid, String rid, final BaseListenner baseListenner) {//敬老院ID
        String url = base + "yanglaoapp/qwadmin/index.php/Home/DailyRectificationsubject";
        if (url == null) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("oid", oid);
        map.put("rid", rid);
        UtilHttp.getInstance().sendPost(url, map, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                try {
                    //Log.e("test", "历史整改2:\t" + result);
                    JSONObject jsonObjectA = new JSONObject(result);
                    JSONArray jsonArray = jsonObjectA.getJSONArray("list");
                    MicrometerTable.getInstance().topicsHostoryRC.clear();
                    if (jsonArray != null && jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            Topic topic = new Topic();
                            String id = jsonObject.getString("id");//ID
                            String question = jsonObject.getString("title");//题目问题
                            String maxScore = jsonObject.getString("subject_score");//分数。最大分数
                            String cscore = jsonObject.getString("scoring");//当前分数

                            if (cscore == null || cscore.trim().equals("")) {
                                cscore = "0";
                            }

                            topic.setId(id);
                            topic.setQuestion(question);
                            topic.setMaxScore(maxScore);
                            topic.setCscore(cscore);
                            topic.setZhengai("2");//计划整改
                            topic.setCount((i + 1) + "");//题号
                            MicrometerTable.getInstance().topicsHostoryRC.add(topic);
                        }
                        //缓存日常表历史整改记录
                        CacheUtils.getInstance().put(MicrometerTable.getInstance().getTopicsHostoryRCMark(), JSONObjectUtils.getInstance().ArrayListToJSON(MicrometerTable.getInstance().topicsHostoryRC));

                        String richang = jsonObjectA.getString("daily_test");//当前分数;
                        Log.e("test", "获取额外字段:\t" + richang);
                        if (richang != null && !richang.trim().equals("") && !richang.trim().equals("null")) {
                            //缓存日常表，上一次选中题目的序号。
                            CacheUtils.getInstance().put(MicrometerTable.getInstance().getTopicsMarkRichang(), richang);
                            MicrometerTable.getInstance().caceRichang();
                        }

                    } else {
                        CacheUtils.getInstance().remove(MicrometerTable.getInstance().getTopicsHostoryRCMark());//清除本地历史整改。以服务器同步
                        Log.e("test", "历史整改为空2");
                    }
                } catch (JSONException e) {
                    Log.e("test", "历史整改JSON数据解析出错2:\t" + e.getMessage());
                }
                if (baseListenner != null) {
                    baseListenner.onResult(null);
                }
            }

            @Override
            public void onFailure(String result) {
                MicrometerTable.getInstance().getHostoryRCTopics();//换取本地缓存
                if (baseListenner != null) {
                    baseListenner.onResult(null);
                }
                Log.e("test", "历史整改服务器反问异常2" + result);
            }
        });
    }

    public void getCategories2(String subject_f, final BaseListenner<List<Categirues2>> baseListenner) {
        Map<String, String> map = new HashMap<>();
        map.put("rid", CreateMicrometerTableDailog2.rid);
        map.put("subject_f", subject_f);//题目大类id
        UtilHttp.getInstance().sendPost(urlCategories2, map, new UtilHttp.RequestCallBack() {
            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                //Log.e("test", "信息返回:\t" + result);
                List<Categirues2> categirues2s = new ArrayList<Categirues2>();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").trim().equals("0")) {
                        JSONArray array = jsonObject.getJSONArray("list");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jsonObject2 = array.getJSONObject(i);
                            String id = jsonObject2.getString("id");
                            String name = jsonObject2.getString("name");
                            Categirues2 categirues2 = new Categirues2();
                            categirues2.setId(id);
                            categirues2.setName(name);
                            categirues2s.add(categirues2);
                        }
                        CacheUtils.getInstance().put("二级菜单", JSONObjectUtils.getInstance().ArrayListToJSON(categirues2s));
                    }
                } catch (Exception e) {
                    Log.e("test", "二级菜单数据解析异常:\t" + e.getMessage());
                }
                if (baseListenner != null) {
                    baseListenner.onResult(categirues2s);
                }
            }

            @Override
            public void onFailure(String result) {
                if (baseListenner != null) {
                    JSONArray jsonArray = CacheUtils.getInstance().getAsJSONArray("二级菜单");
                    List<Categirues2> categirues2s = new ArrayList<Categirues2>();
                    if (jsonArray != null) {
                        try {
                            categirues2s = JSONObjectUtils.getInstance().getArrayList(jsonArray, Categirues2.class);
                        } catch (Exception e) {
                            Log.e("test", "二级菜单数据解析异常2:\t" + e.getMessage());
                        }
                    }
                    baseListenner.onResult(categirues2s);
                }
            }
        });

    }

}
